package org.dawn.deveco.plugin.resourceeditor.test.buttonTest;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

public class JButtonTableExample extends JFrame {
    public JButtonTableExample() throws Exception {
        super("JButtonTable Example - jb51.net");

        String url = getClass().getResource("/icon/addItem_32x32.png").getFile();
        File file = new File(url);
        System.out.println(url);
        System.out.println(file.getAbsolutePath());

        Icon icon1 = new ImageIcon(getClass().getResource("/icon/addItem_32x32.png").toURI().toURL());
        Icon icon2 = new ImageIcon(getClass().getResource("/icon/deleteItem_48x48.png").toURI().toURL());
        System.out.println(icon1.getIconHeight());

        //添加TableModel以及表格数据
        DefaultTableModel dm = new DefaultTableModel();
        dm.setDataVector(new Object[][]{
                {"button 1", "foo", icon1},
                {"button 2", "bar", icon2}
        }, new Object[]{"Button", "String", "Icon"});

        JTable table = new JTable(dm);

        //添加渲染器
        table.getColumn("Button").setCellRenderer(new ButtonRenderer());

        //添加编辑器
        table.getColumn("Button").setCellEditor(new ButtonEditor(new JCheckBox()));

        // 创建一个JLabel，并在其中添加图片
        JLabel label = new JLabel();
        label.setText("Hello");
        label.setIcon(icon1);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setSize(200, 100);

        //GUI设置
        JScrollPane scroll = new JScrollPane(table);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(scroll, BorderLayout.NORTH);
        getContentPane().add(label, BorderLayout.CENTER);

        setSize(400, 400);

        pack();
        setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) throws Exception {
        JButtonTableExample frame = new JButtonTableExample();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}